// pages/micro/micro.js
var app = getApp();

//HTTP请求request封装
var HttpRequest = require("../../http.js");
//CBC加密 
var CryptoJS = require("../../Crypto.js");
//RSA加密
var RSA = require("../../rsa.js");
//qrcode生成二维码
var QRCodeJS = require("../../utils/qrcode.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
      name:'',//姓名
      phone:'',//电话号码
      cord:'',//身份证号码
      image:'',//图片
  },

  getReq: HttpRequest.getReq,//GET数据请求函数
  postReq: HttpRequest.postReq,//POST数据请求
  header: HttpRequest.header,//header参数

  Bank:function(e){
    wx.navigateTo({
      url: '../applys/applys'
    })
  },
  BankBd:function(){
    wx.switchTab({
      url: '../Health/Health'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) { 
    var that = this;
    const login_Info = JSON.parse(wx.getStorageSync('login_Information')); //登录基础信息
    const headArray = wx.getStorageSync('UserLogin');
    const headmode = {
      'Accept': 'application/json',
      "Content-Type": "application/json;charset=UTF-8",
      "headMode": headArray,
      "token": login_Info.token,
    };
    var InfoData = {};
    const infoUrl = '/resident/healthcard/info';
    //随机私钥  
    const key = CryptoJS.enc.Utf8.parse(app.globalData.PrivateKey);

    //加密偏移量
    const options = {
      iv: CryptoJS.enc.Utf8.parse("bskyapp123456789"),
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    };
    HttpRequest.getReq(infoUrl, InfoData, headmode, function (res, code) {
      console.log(res);
      if (code == 200) {  
        //CBC解密卡号
        var decryptedData = CryptoJS.AES.decrypt(res.zjhm, key, options);
        var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
        console.log('CBC解密：', decryptedStr);
        const cordId = decryptedStr;
        const cordId_hm = cordId.substr(0, 3) + '****' + cordId.substr(14);
        //CBC解密姓名
        var decryptedName = CryptoJS.AES.decrypt(res.realName, key, options);
        var decryptedStrName = decryptedName.toString(CryptoJS.enc.Utf8);
        const realName = decryptedStrName
        //CBC解密电话号码
        var decryptedPhone = CryptoJS.AES.decrypt(res.lxdh, key, options);
        var decryptedStrPhone = decryptedPhone.toString(CryptoJS.enc.Utf8);
        const phone = decryptedStrPhone;
        console.log(phone);
        const lxdh_hm = phone.substr(0, 3) + '****' + phone.substr(7);
        //CBC解密二维码图片
        var Base64 = res.base64;
        //console.log(Base64.search("\n") == -1);
        if (!(Base64.search("\n") == -1)) {
          Base64 = Base64.replace("\n", "")
        }
        //console.log("BaseImage:", Base64);
        var decryptedimage = CryptoJS.AES.decrypt(Base64, key, options);
        var decryptedStrimage = decryptedimage.toString(CryptoJS.enc.Utf8);
        console.log('CBC解密：', decryptedStrimage);
        const baseImage = decryptedStrimage;

        /**
     * 绘制带logo二维码
     * @param url        二维码字符串 如 https://github.com/xlsn0w
     * @param canvas-id  画布ID 如 logoQRCode
     * @param width      二维码宽度 如 275
     * @param height     二维码高度 如 275
     * @param logo       二维码logo 如 /images/xlsn0w.png
     */
        QRCodeJS.qrApi.draw(baseImage, "logoQRCodeBig", 250, 250, null, '../../pages/image/redTen.jpg');

          that.setData({
            //登录基础信息回写
            name: realName,//用户姓名
            cord: cordId_hm,
            phone: lxdh_hm,
            image: res.base64,
          })
        } else {
          wx.navigateTo({
            url: '../apply/apply'
          })
        }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})